package com.xxxx.controller;


import com.xxxx.pojo.Admin;
import com.xxxx.pojo.R;
import com.xxxx.pojo.Role;
import com.xxxx.service.AdminService;
import com.xxxx.service.RoleService;
import com.xxxx.service.impl.RoleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chao
 * @since 2022-09-26
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "获取所有操作员")
    @GetMapping("/")
    public R getAllAdmins(String keywords) {
        return adminService.getAllAdmins(keywords);
    }

    @ApiOperation(value = "更新操作员")
    @PutMapping("/")
    public R updateAdmin(@RequestBody Admin admin) {
        if (adminService.updateById(admin)) {
            return R.success("更新成功！");
        }
        return R.error("更新失败！");
    }

    @ApiOperation(value = "删除操作员")
    @DeleteMapping("/{id}")
    public R deleteAdmin(@PathVariable Integer id) {
        boolean flag = adminService.removeById(id);
        return flag ? R.success("删除成功！") : R.error("删除失败！");
    }

    @ApiOperation(value = "获取所有角色")
    @GetMapping("/roles")
    public R getAllRoles() {
        List<Role> roles = roleService.list();
        return R.success(roles);
    }

    @ApiOperation(value = "更新操作员角色")
    @PutMapping("/role")
    public R updateAdminRole(Integer adminId, Integer[] rids) {
        return adminService.update(adminId, rids);
    }
}
